package com.bjpowernode.controller;
import com.bjpowernode.beans.MyBeans;
import com.bjpowernode.beans.Stu;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.MyDateExceptionAdvice;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@RestController
@RequestMapping("/")
public class IndexController {

    @RequestMapping("get.action")
    public ResultDTO get(Stu stu, BindingResult bindingResult) throws MyDateExceptionAdvice {
        if(bindingResult.hasErrors()){
            throw new MyDateExceptionAdvice("生日格式输入错误");
        }
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("name",stu.getName());
        hashMap.put("age",bindingResult.getFieldValue("age").toString());
        resultDTO.setData(hashMap);
        return resultDTO;
    }

    @RequestMapping("getAA.action")
    public ResultDTO getAA(@Valid MyBeans myBeans, BindingResult bindingResult)  {
        if(bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().toString());

            System.out.println(bindingResult.getFieldError().getDefaultMessage());

        }
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }

}
